using System;
using System.Web;
using System.Security.Cryptography;

namespace Subgurim.Controles.SSM
{
    /// <summary>
    /// Summary description for SiteMapUtilities
    /// </summary>
    internal class SubgurimSiteMapUtilities
    {
        internal SubgurimSiteMapUtilities()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        internal static bool IAmAutoCalling(HttpContext context)
        {
            return !string.IsNullOrEmpty(context.Request.QueryString[SubgurimSiteMapQuery]);
        }

        internal static string SubgurimSiteMapQuery = "SubgurimSiteMapQuery";

        internal static string takeLocalPath()
        {
            string local = HttpContext.Current.Request.Url.LocalPath;
            local = HttpContext.Current.Request.Url.Authority.Contains("localhost:") ? local.Substring(local.IndexOf("/", 1)) : local;
            local = local.TrimStart(new char[] { '/' });

            return local.ToLower();
        }

        internal static string takeLocalRawUrl()
        {
            var local = HttpContext.Current.Request.RawUrl;
            local = HttpContext.Current.Request.Url.Authority.Contains("localhost:") ? local.Substring(local.IndexOf("/", 0, StringComparison.Ordinal)) : local;
            local = local.TrimStart(new[] { '/' });

            return local.ToLower();
        }

        internal static string fixToLocalPath(string inner)
        {
            return inner.ToLower().Replace("~/", "");
        }

        internal static string getHash(string textoPlano)
        {
            byte[] data = System.Text.UTF8Encoding.ASCII.GetBytes(textoPlano);
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] hashbyte = md5.ComputeHash(data, 0, data.Length);
            return BitConverter.ToString(hashbyte);
        }

        internal static string getDelim(string loc)
        {
            string delim = "?";
            if (loc.Contains("?"))
                delim = "&";

            return delim;
        }

        internal static void showOnScreen(string text, HttpResponse Response)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.Flush();
            Response.Write(text);
            Response.End();
        }

        internal static string getCleanNodeValue(string nodeValue)
        {
            return nodeValue.Trim(new char[] { ' ', '\r', '\n' });
        }

        internal static string makeRelative(string baseUrl, string relativeUrl)
        {
            baseUrl = baseUrl.Trim(new char[] { '/' });
            relativeUrl = relativeUrl.Trim(new char[] { '/' });

            int ipuntosBarra = 0;
            string spuntosBarra ="../";
            while (relativeUrl.StartsWith(spuntosBarra))
            {
                ipuntosBarra++;
                relativeUrl = relativeUrl.Substring(0, spuntosBarra.Length);
            }

            for (int i = 0; i < ipuntosBarra; i++ )
            {
                baseUrl = baseUrl.Substring(0, baseUrl.LastIndexOf('/'));
            }

            return baseUrl + "/" + relativeUrl;
        }
    }
}